<?php

namespace App\Repository\Admin;

use App\Models\Admin\AdminUsersMoneyLog as model;
use App\Repository\BaseRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\Admin\AdminUsersRepository;


class AdminUsersLogRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    $this->user = new AdminUsersRepository();
    parent::__construct();
  }

  public function index($request)
  {
    if ($request->user->role_id != 1) {
      $cron['where']['shop_id'] = $request->user->shop_id;
    }

    if (!empty($request['dateRange'][0]) && !empty($request['dateRange'][0])) {
      $cron['between']['create_time'][] = $request['dateRange'][0];
      $cron['between']['create_time'][] = $request['dateRange'][1];
    }
    if (isset($request['type'])) {
      $cron['where']['type'] = $request->get('type');
    }
    if (!empty($request->get('orderno'))) {
      $cron['like']['orderno'] = $request->get('orderno');
    }
    $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
    $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
    $pageNums = 0;
    $cron['limit']['number'] = isset($request['pagenums']) ? ($request['page_size']) : 10;
    if (isset($request['page']) && !empty(($request['page']))) {
      $pageNums = ($request['page'] - 1) * $cron['limit']['number'];
    }
    $cron['limit']['start'] = $pageNums;
    $cron['select'] = [('money'), 'ordernum', 'type', 'id', 'type_desc', 'create_time'];
    $data = $this->search($this->model, $cron, 1, 10);
    if (!empty($data['data'])) {
      foreach ($data['data'] as $key => $v) {
        $data['data'][$key] = usfirstArr($v);
      }
    }
    return  $data;
  }
  //添加
  public function store($data)
  {
    //必填 字段
    $mustField = [
      'ordernum',
      'money',
      'shop_id',
      'type',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'ordernum' => '账号不能为空',
      'money' => '金额不能为空',
      'shop_id' => '用户不能为空',
      'type' => '收费类型不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    if(empty($insertData['type'])){
      $insertData['type'] = 0;
    }
    //判断用户是否存在
    $isTrue = $this->model->where(['ordernum' => $insertData['ordernum'], 'type' => $insertData['type'], 'shop_id' => $insertData['shop_id']])->first();
    if ($isTrue && $insertData['type'] != 3) {
      return $this->returnMsg(200, '该款项已扣除');
    }
    if(empty($insertData['money'])){
      $insertData['money'] = 0.01;
    }
    // 开启事务
    DB::beginTransaction();
    $isUpdate = $this->user->deductMoney($insertData['shop_id'], $insertData['money']);
    // var_dump($isUpdate);
    $insertData['create_time'] = date('Y-m-d H:i:s');
    if (is_array($isUpdate)) {
      $insertData = array_merge($insertData, $isUpdate);
    } else {
      DB::rollback();
      return $this->returnMsg(1, !empty($isUpdate) ? $isUpdate : '资金不足');
    }
    if (!empty($insertData['type'])) {
      $insertData['type_desc'] = $this->model->getTypeDesc($insertData['type']);
    }

    $res = $this->model->insert($insertData);

    if ($res && $isUpdate) {
      DB::commit();
      return $this->returnMsg(200, '添加成功');
    }
    DB::rollback();
    return $this->returnMsg(1, '添加失败');
  }
  //查询 单条数据
  public function oneSel($where, $isUp = true, $field = ['*'])
  {
    $data =  $this->model->where($where)->select($field)->first();
    if (!empty($data) && $isUp) {
      $data = usfirstArr($data);
    }
    return $data;
  }
}
